package com.huika.dealontop.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.huika.dealontop.R;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/18 14:13
 * Description: 仿IOS样式的对话框
 */
    public class IOSAlertDialog extends Dialog implements View.OnClickListener {

    private View mDialogView;
    private TextView mTvTitle;
    private TextView mTvContentText;
    private FrameLayout mFlCustomArea;

    private TextView mTvCancelBtn;
    private TextView mTvConfirmBtn;

    private View mFootLine;
    private View mBetweenLine;

    private String mTitleText;
    private String mContentText;
    private String mCancelText;
    private String mConfirmText;

    private boolean mShowContent;
    private boolean mShowCancel;
    private boolean mShowTitle;
    private boolean mShowCustom;
    private View mCustomView;

    private OnClickListener mCancelClickListener;
    private OnClickListener mConfirmClickListener;

    public IOSAlertDialog(Context context) {
        super(context, R.style.MMTheme);
        //默认可取消
        setCancelable(true);
        //点击外围不取消
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog);
        mDialogView = getWindow().getDecorView().findViewById(
                android.R.id.content);
        mTvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        mTvContentText = (TextView) findViewById(R.id.tv_dialog_content_text);
        mFlCustomArea = (FrameLayout) findViewById(R.id.custom_frame);

        mTvCancelBtn = (TextView) findViewById(R.id.cancel_button);
        mTvConfirmBtn = (TextView) findViewById(R.id.confirm_button);

        mFootLine = findViewById(R.id.line_foot);
        mBetweenLine = findViewById(R.id.line_between_cancel_confirm);

        mTvConfirmBtn.setOnClickListener(this);
        mTvCancelBtn.setOnClickListener(this);

        setTitleText(mTitleText);
        setContentText(mContentText);
        setCancelText(mCancelText);
        setConfirmText(mConfirmText);

    }

    public IOSAlertDialog setConfirmText(String text) {
        mConfirmText = text;
        if (mTvConfirmBtn != null && mConfirmText != null) {
            mTvConfirmBtn.setText(mConfirmText);
        }
        return this;
    }

    public IOSAlertDialog setCancelText(String text) {
        mCancelText = text;
        if (mTvCancelBtn != null && mCancelText != null) {
            showCancelButton(true);
            mTvCancelBtn.setText(mCancelText);
        }
        return this;
    }

    private void showCancelButton(boolean isShow) {
        mShowCancel = isShow;
        if (mTvCancelBtn != null) {
            mTvCancelBtn.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);

            //同时也显示中间分割线
            if (mBetweenLine != null) {
                mBetweenLine.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
            }
            //改变确认按钮 背景
            if (mTvConfirmBtn != null) {
                mTvConfirmBtn.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.selector_dialog_right_btn_bg));
            }
        }
    }

    public IOSAlertDialog setContentText(String text) {
        mContentText = text;
        if (mTvContentText != null && mContentText != null) {
            showContentText(true);
            mTvContentText.setText(mContentText);
        }
        return this;
    }

    private IOSAlertDialog showContentText(boolean isShow) {
        mShowContent = isShow;
        if (mTvContentText != null) {
            mTvContentText.setVisibility(mShowContent ? View.VISIBLE
                    : View.GONE);
        }
        return this;
    }

    public IOSAlertDialog setTitleText(String text) {
        mTitleText = text;
        if (mTvTitle != null && mTitleText != null) {
            showTitleText(true);
            mTvTitle.setText(mTitleText);
        }
        return this;
    }

    private IOSAlertDialog showTitleText(boolean isShow) {
        mShowTitle = isShow;
        if (mTvTitle != null) {
            mTvTitle.setVisibility(mShowTitle ? View.VISIBLE
                    : View.GONE);
        }
        return this;
    }

    public IOSAlertDialog setCustomView(View view) {
        return setCustomView(view, null);
    }

    public IOSAlertDialog setCustomView(View view, FrameLayout.LayoutParams params) {
        mCustomView = view;
        if (mFlCustomArea != null && mCustomView != null) {
            showCustomView(true);
            if(params != null){
                mFlCustomArea.addView(mCustomView, params);
            }else{
                mFlCustomArea.addView(mCustomView);
            }
        }
        return this;
    }

    private IOSAlertDialog showCustomView(boolean isShow) {
        mShowCustom = isShow;
        if (mFlCustomArea != null) {
            mFlCustomArea.setVisibility(mShowCustom ? View.VISIBLE
                    : View.GONE);
        }
        return this;
    }

    public OnClickListener getConfirmClickListener() {
        return this.mConfirmClickListener;
    }

    public IOSAlertDialog setConfirmClickListener(OnClickListener confirmClickListener) {
        this.mConfirmClickListener = confirmClickListener;
        return this;
    }

    public OnClickListener getCancelClickListener() {
        return this.mCancelClickListener;
    }

    public IOSAlertDialog setCancelClickListener(OnClickListener cancelClickListener) {
        this.mCancelClickListener = cancelClickListener;
        return this;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.cancel_button) {
            if (mCancelClickListener != null) {
                mCancelClickListener.onClick(IOSAlertDialog.this, view.getId());
            } else {
                dismiss();
            }
        } else if (view.getId() == R.id.confirm_button) {
            if (mConfirmClickListener != null) {
                mConfirmClickListener.onClick(IOSAlertDialog.this, view.getId());
            } else {
                dismiss();
            }
        }
    }

}
